<template>
  <div style="width:100%;height:213px;"
       id="smoothLine"></div>
</template>

<script>
import echarts from 'echarts'

export default {
  name: 'dataSourcePie',
  props: {
    LineDataSource: {
      type: Array,
      default: function() {
        return []
      }
    }
  },
  data() {
    return {}
  },
  watch: {
    LineDataSource(curVal, oldVal) {
      this.initEchats()
    }
  },
  mounted() {},
  methods: {
    initEchats() {
      var LineDataSource = echarts.init(document.getElementById('smoothLine'))
      var fullMonthArray = [
        '一月',
        '二月',
        '三月',
        '四月',
        '五月',
        '六月',
        '七月',
        '八月',
        '九月',
        '十月',
        '十一月',
        '十二月'
      ]
      var nowMonth = new Date().getMonth() + 1
      var xAxisData = fullMonthArray.slice(0, nowMonth)
      var dangerAlarmData = []
      var passengerAlarmData = []
      var generalAlarmData = []
      var travelAlarmData = []
      for (let i = 0; i < nowMonth; i++) {
        let dataItem = this.LineDataSource.find(val => val.mon == i + 1)
        dangerAlarmData.push(dataItem.dangerAlarmCount)
        passengerAlarmData.push(dataItem.passengerAlarmCount)
        generalAlarmData.push(dataItem.generalAlarmCount)
        travelAlarmData.push(dataItem.travelAlarmCount)
      }

      let option = {
        color: [
          '#5867dd',
          '#1dc9b7',
          '#2786fb',
          '#ffb822',
          '#fd27eb',
          '#6610f2',
          '#6f42c1',
          '#e83e8c',
          '#dc3545',
          '#fd7e14',
          '#ffc107',
          '#28a745',
          '#20c997',
          '#17a2b8',
          '#e1e1ef',
          '#007bff'
        ],
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['危险货运', '班线客运', '普通货运', '旅游客运']
        },
        xAxis: {
          type: 'category',
          boundaryGap: false,
          data: xAxisData
        },
        yAxis: {
          type: 'value',
          // interval: 1,
          axisLabel: {
            formatter: '{value}'
          },
          axisLine: {
            //---坐标轴 轴线
            show: true, //---是否显示
            //------------------- 箭头 -------------------------
            symbol: ['none', 'arrow'], //---是否显示轴线箭头
            symbolSize: [8, 8], //---箭头大小
            // symbolOffset: [0, 7], //---箭头位置

            //------------------- 线 -------------------------
            lineStyle: {
              color: '#7f8081',
              width: 1,
              type: 'solid'
            }
          },
          splitLine: {
            show: true,
            lineStyle: {
              type: 'dashed'
            }
          }
        },
        series: [
          {
            name: '危险货运',
            type: 'line',
            data: dangerAlarmData,
            smooth: true
          },
          {
            name: '班线客运',
            type: 'line',
            data: passengerAlarmData,
            smooth: true
          },
          {
            name: '普通货运',
            type: 'line',
            data: generalAlarmData,
            smooth: true
          },
          {
            name: '旅游客运',
            type: 'line',
            data: travelAlarmData,
            smooth: true
          }
        ]
      }

      LineDataSource.setOption(option)
      window.addEventListener('resize', function() {
        LineDataSource.resize()
      })
    }
  }
}
</script>
